<?php

namespace Controller\Ajax;

class Passport extends Base {

    /**
     * 判断手机号后发送验证码
     */
    public function checkPhoneAndSendMessageVerify() {
        $phone = $this->postParam('phone');
        $mask = $this->getUserMask();
        $ip = $this->getIp();
        try {
            //$isOk = \Business\User::instance()->checkPhoneAndSendMessageVerify($phone, $mask, $ip);
            $isOk = true;
            if($isOk) {
                $this->response(true);
            }
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 判断验证码
     */
    public function checkVerifyByPhone(){
        $verify = $this->postParam('verify');
        $phone = $this->postParam('phone');
        $mask = $this->getUserMask();
        try {
            //$isOk = \Business\User::instance()->checkMessageVerify($verify, $mask, $phone);
            $isOk = true;
            if($isOk) {
                $this->response(true);
            }
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }
    }

    public function login() {
        $phone = $this->postParam('phone');
        $password = $this->postParam('password');
        if(!$phone) {
            $this->responseError('请填写手机号');
        }
        if(!$password) {
            $this->responseError('请填写密码');
        }
        $info = \Business\User::instance()->getUserInfoByPhone($phone);
        if(!$info) {
            $this->responseError(\Util\User::encodePassword($password));
            $this->responseError('没有该用户');
        }
        if(\Util\User::encodePassword($password) != $info['Password']) {
            $this->responseError('密码不正确');
        }
        $this->response(true);
    }

    /**
     * 用户注册
     */
    public function registerByPhone() {
        $phone = $this->postParam('phone');
        $password = $this->postParam('password');
        $nickname = $this->postParam('nickname');
        $mask = $this->getUserMask();
        $ip = $this->getIp();
        try {
            $id = \Business\User::instance()->registerByPhone($phone, $password, $nickname, $mask, $ip);
            if($id) {
                $this->response($id);
            } else {
                $this->responseError('注册失败！');
            }
        } catch (\Lib\Exception\Business $e) {
            $this->responseError($e->getMessage(), $e->getCode());
        }

    }
}
